﻿namespace CodeSHARPer.View.Tools
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    /// <summary>
    /// Interaction logic for FindReplaceView.xaml
    /// </summary>
    public partial class FindReplaceView : UserControl
    {
        private TextBox mTxtFind = null;
        private TextBox mTxtFind2 = null;
        private TextBox mTxtReplace = null;

        public FindReplaceView()
        {
            this.InitializeComponent();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            try
            {
                this.mTxtFind = this.GetTemplateChild("PART_TxtFind") as TextBox;
                this.mTxtFind2 = this.GetTemplateChild("PART_TxtFind2") as TextBox;
                this.mTxtReplace = this.GetTemplateChild("PART_Replace") as TextBox;

                // Setting focus into each textbox control is controlled via viewmodel and attached property
                // Each textbox selects all content (by default) when it aquires the focus
                if (this.mTxtFind != null)
                {
                    this.mTxtFind.GotKeyboardFocus += (s, e) =>
                    {
                        this.mTxtFind.SelectAll();
                    };
                }

                if (this.mTxtFind2 != null)
                {
                    this.mTxtFind2.GotKeyboardFocus += (s, e) =>
                    {
                        this.mTxtFind2.SelectAll();
                    };
                }

                if (this.mTxtReplace != null)
                {
                    this.mTxtReplace.GotKeyboardFocus += (s, e) =>
                    {
                        this.mTxtReplace.SelectAll();
                    };
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}
